﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace mVu
{
  class YouPorn : Site
  {
    public override Dictionary<string, string> GetCategories()
    {
      var categories = new Dictionary<string, string>
                         {
                           {String.Empty, "http://www.youporn.com/?user_choice=Enter"}
                         };
      return categories;
    }

    public override Dictionary<string, string> GetSorts()
    {
      var sorts = new Dictionary<string, string>
                    {
                      {String.Empty, String.Empty}
                    };

      return sorts;
    }

    public override Dictionary<string, string> GetMediaTypes()
    {
      var types = new Dictionary<string, string>
                    {
                      {"FLV", "FLV"}
                    };
      return types;
    }

    public override List<Thumbnail> GetThumbnails(string baseAddress, string sort, string query, int page)
    {
      var list = new List<Thumbnail>();

      if (String.IsNullOrEmpty(baseAddress))
      {
        baseAddress = "http://www.youporn.com/?user_choice=Enter";
      }

      string address = String.IsNullOrEmpty(query) ? baseAddress : baseAddress + "&search=" + query;
      string result = GetPageSource(address);

      if (!String.IsNullOrEmpty(result))
      {
        var rh = new SgmlReaderDll.SGMLReaderHelper();
        string xhtml = rh.ProcessString(result);

        XDocument doc = XDocument.Parse(xhtml);
        XNamespace xmlns = XNamespace.Get("http://www.w3.org/1999/xhtml");

        var nodes = doc.Descendants(xmlns + "div").Where(n => n.Attribute("id") != null && n.Attribute("id").Value == "video-listing").Descendants(xmlns + "li");

        list.AddRange(nodes.Select(node => new Thumbnail
                                             {
                                               Url = String.Format("http://www.youporn.com{0}", node.Descendants(xmlns + "a").Attributes("href").First().Value),
                                               VideoID = String.Format("http://www.youporn.com{0}", node.Descendants(xmlns + "a").Attributes("href").First().Value),
                                               Title = node.Descendants(xmlns + "img").Attributes("alt").First().Value,
                                               Image = node.Descendants(xmlns + "img").Attributes("src").First().Value
                                             }));
      }

      return list;
    }

    public override Dictionary<string, string> GetVideoPaths(string url)
    {
      var dictionary = new Dictionary<string, string>();

      if (!String.IsNullOrEmpty(url))
      {
        string result = GetPageSource(url);

        if (!String.IsNullOrEmpty(result))
        {
          int start = result.IndexOf("\"http://download") + 1;
          int end = result.IndexOf("\"", start + 1);

          string path = result.Substring(start, end - start);
          dictionary.Add("FLV", path);
        }
      }

      return dictionary;
    }
  }
}
